<?php
/**
 * @category 	MenuDao.php
 * @author 		Sung Hoon, Cho (akdddnet@gmail.com)
 * @since		2011. 10. 20.
 * @license		BSD License
 * @version		0.1
 */

global $CFG;
require_once $CFG->FW_DIR."/mvc/Dao.php";

class MenuDao extends Dao {
	function __construct(array $db_type = null) {
		parent::__construct($db_type);
	}	
	
	
	function index(DTO $dto) {
		$query = "
					SELECT		*
					FROM		menu
					WHERE		1 = 1
					ORDER BY mn_order, mn_seq
		";
		
		$dto->list = $this->db->query($query)->fetchAll();
		
		return $dto;
	}
	
	
	function doAdd(DTO $dto) {
		$query = "
				INSERT INTO menu
					(mn_name, mn_icon, mn_url, mn_external, mn_order)
				VALUES
					(
						 :mn_name
						,:mn_icon
						,:mn_url
						,:mn_external
						,(
							SELECT orders FROM (
								SELECT MAX(mn_order)+1 AS orders FROM menu WHERE mn_admin > 0
							) a
						)
					)
		";
		
		$bind = array(
				"mn_name"=>$dto->eb->mn_name
				,"mn_icon"=>$dto->eb->mn_icon
				,"mn_url"=>$dto->eb->mn_url
				,"mn_external"=>$dto->eb->mn_external
		);
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	
	function doDelete(DTO $dto) {
		$query = "
					DELETE FROM menu
					WHERE		1 = 1
					AND			mn_seq = :mn_seq
					AND			mn_delete > 0
					
		";
		
		$bind = array(	
					"mn_seq"=>$dto->eb->mn_seq
		);
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	
	function view(DTO $dto) {
		$query = "
				SELECT
							*
				FROM		menu
				WHERE		1 = 1
				AND			mn_seq = :mn_seq
		";
		
		$bind = array(
				"mn_seq"=>$dto->eb->mn_seq
		);
		
		
		$dto->obj = $this->db->query($query, $bind)->fetch();
		
		return $dto;
	}
	
	
	
	function doModify(DTO $dto) {
		$query = "
					UPDATE menu SET
							 mn_name 		= :mn_name
							,mn_url 		= :mn_url
							,mn_icon		= :mn_icon
							,mn_external	= :mn_external
							,mn_order		= :mn_order
					WHERE	1 = 1
					AND		mn_seq = :mn_seq
		";
		
		
		$bind = array(
					"mn_name"=>$dto->eb->mn_name
					,"mn_url"=>$dto->eb->mn_url
					,"mn_icon"=>$dto->eb->mn_icon
					,"mn_external"=>$dto->eb->mn_external
					,"mn_order"=>$dto->eb->mn_order
					,"mn_seq"=>$dto->eb->mn_seq
		);
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	function getMenu(DTO $dto) {
		$query = "
					SELECT 		*
					FROM		menu
					WHERE		1 = 1
					AND			mn_type = :mn_type
					AND			mn_num	= :mn_num
		";
		
		$bind = array(
				 "mn_type"=>$dto->eb->mn_type
				,"mn_num"=>$dto->eb->mn_num
		);
		
		
		$dto->obj = $this->db->query($query, $bind)->fetch();
		
		return $dto;
	}
	
	
	/**
	 * 게시판 관리 -> 메뉴 등록
	 * 
	 * @param DTO $dto
	 * @return DTO
	 */
	function doExternalAdd(DTO $dto) {
		$query = "
			INSERT INTO menu
				(mn_name, mn_icon, mn_url, mn_type, mn_num, mn_order)
 			VALUES
 				(
 					 :mn_name
 					,:mn_icon
 					,:mn_url
 					,:mn_type
 					,:mn_num
 					,(
						SELECT orders FROM (
							SELECT MAX(mn_order)+1 AS orders FROM menu WHERE mn_admin > 0
						) a
					)
 				)
 		";
		
		$bind = array(
				 "mn_name"=>$dto->eb->mn_name
				,"mn_icon"=>$dto->eb->mn_icon
				,"mn_url"=>$dto->eb->mn_url
				,"mn_type"=>$dto->eb->mn_type
				,"mn_num"=>$dto->eb->mn_num
		);
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	
	
	function doexternalModify(DTO $dto) {
		$query = "
					UPDATE menu SET
						mn_name = :mn_name
						,mn_icon = :mn_icon
					WHERE	1 = 1
					AND		mn_type = :mn_type
					AND		mn_num	= :mn_num
		";
		
		$bind = array(
			 "mn_name"=>$dto->eb->mn_name
			,"mn_icon"=>$dto->eb->mn_icon
			,"mn_type"=>$dto->eb->mn_type
			,"mn_num"=>$dto->eb->mn_num
		);
		
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	
	/**
	 * 게시판 삭제 또는 외부에 특정 메뉴를 삭제 할 경우 사용되는 함수
	 * 
	 * @param DTO $dto
	 */
	function doExternalDelete(DTO $dto) {
		$query = "
				DELETE FROM menu
				WHERE	1 = 1
				AND		mn_type = :mn_type
				AND		mn_num  = :mn_num
		";
		
		$bind = array(
			"mn_type"=>$dto->eb->mn_type
			,"mn_num"=>$dto->eb->mn_num
		);
		
		
		$dto->rc = $this->db->query($query, $bind)->rowCount();
		
		return $dto;
	}
	
	
	/**
	 * 사이트 메뉴 리스트
	 * 
	 * @param DTO $dto
	 * @return DTO
	 */
	function externalMenuList(DTO $dto) {
			
		$query = "
		 					SELECT 
		 								*
		 					FROM		menu
		 					WHERE		1 = 1
		 					AND			mn_admin >= :level
		 					ORDER BY mn_order, mn_seq
		 	";
	
		// 로그인이 안되어 있다면 아무 메뉴도 표시 안되게 2값을 설정.
		$level = (Common::isNullOrEmpty($_SESSION['owner'])) ? 2 : $_SESSION['owner']->m_level;
		$param = array('level'=>$level);
			
		$dto->list = $this->db->query($query, $param)->fetchAll();
			
		return $dto;
	}
}